import Vue from 'vue'
import Router from 'vue-router'
// 引入多个模块的规则
import departmentsRouter from './modules/departments'// 组织架构
import employeesRouter from './modules/employees'// 员工管理
import settingRouter from './modules/setting'// 公司设置
import permissionRouter from './modules/permission'// 权限管理
import socialRouter from './modules/social'// 社保
import attendancesRouter from './modules/attendances'// 考勤
import salarysRouter from './modules/salarys'// 工资
import approvalsRouter from './modules/approvals'// 审批
import userRouter from './modules/user'
Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) { return originalPush.call(this, location, onResolve, onReject) }
  return originalPush.call(this, location).catch((err) => err)
}
/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }
    ]
  },
  {
    path: '/import',
    component: Layout,
    hidden: true, // 不显示在左侧菜单中
    children: [{
      path: '', // 二级路由path什么都不写 表示二级默认路由
      component: () => import('@/views/import')
    }]
  },
  userRouter// 放置一个都可以访问的路由
  // { path: '*', redirect: '/404', hidden: true }
]

// 动态路由
export const asyncRoutes = [
  departmentsRouter, // 组织架构
  employeesRouter, // 员工管理
  settingRouter, // 公司设置
  permissionRouter, // 权限管理
  socialRouter, // 社保
  attendancesRouter, // 考勤
  salarysRouter, // 工资
  approvalsRouter// 审批
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  base: 'hr/', // 配置项目的基础地址
  scrollBehavior: () => ({ y: 0 }),
  // routes: [...constantRoutes, ...asyncRoutes]// 静态路由和动态路由的临时合并
  routes: [...constantRoutes]
})

const router = createRouter() // 实例化一个路由

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
